package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 中国信鸽信息网
 * @author Persh
 *
 */
public class ChinaxingeNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://[a-z]*.chinaxinge.com/detail.asp\\?sjm=[a-z0-9]*&id=[0-9]*",
		"http://[a-z]*.chinaxinge.com/detail.asp\\?id=[0-9]*&sjm=[a-z0-9]*",
		"http://[a-z]*.chinaxinge.com/.*/show/\\?newsid=[0-9]*&id=[0-9]*",
		"http://news.chinaxinge.com/shownews.asp\\?sjm=[a-z0-9]*&id=[0-9]*",
		"http://news.chinaxinge.com/shownews.asp\\?id=[0-9]*&sjm=[a-z0-9]*",
		"http://gdgs.chinaxinge.com/.*/show/\\?id=[0-9]*&newsid=[0-9]*",
		"http://www.chinaxinge.com/diary/show_diary.asp\\?id=[0-9]*"
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);
		String title=null;
		String content=null;
		Long date=null;
		String author=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("td[height=55] b").text();

		if(StringUtils.isBlank(title)){
			title =doc.select("td[align=center] b").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("td.un b").text();
			}
		}
		content=HtmlCleaner.getContentHtml(url ,doc.select("td.ee"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url ,doc.select("span#BodyLabel"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url ,doc.select("font.ffce"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url ,doc.select("td[align=left]"));
		}
		date=DateUtils.matchDate(doc.select("td[align=center]").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("td.un").text());
		}

		author=doc.select("td[height=55]").text();
		author=StringUtils.substringAfter(author, "　");
		if(StringUtils.isEmpty(author)){
			author=doc.select("td.un a").text();
			author=StringUtils.substringBefore(author," ");
		}
		if(StringUtils.startsWith(url, "http://www.chinaxinge.com/diary/")){
			Elements select=doc.select("td.input02 font");
			title=select.get(0).text();
			date=DateUtils.matchDate(doc.select("td.rrc font").text());
			content=HtmlCleaner.getContentHtml(url ,doc.select("td.input02 font").first());
			author=doc.select("a[target=_blank]").text();
			author=StringUtils.substringBefore(author," ");
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);
		news.setAuthor(author);

		return news;
	}

	public static void main(String[] args) {
		ChinaxingeNewsAnalyse analyse=new ChinaxingeNewsAnalyse();
		String url="http://www.chinaxinge.com/diary/show_diary.asp?id=97678";
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
